﻿var projectName;
var brandSeriesList; //存储品牌和车系内容
$(function () {
    projectName = $('#projectName').attr('project');

    /*设置添加客户的生日格式*/
    $('.cartime_format').datetimepicker({
        format: 'YYYY/MM/DD'
    });

    /**
     * 获取左侧树的数据
     * @returns {[]}
     */
    function getTree() {
        // Some logic to retrieve, or generate tree structure
        var data = [];
        $.ajax({
            url: projectName + '/repertory/brand/tree.do',
            success: function (result) {
                if (result.code == 200) {
                    data = result.data;
                }
            },
            async: false  //设置ajax为同步请求  必须等待数据加载完毕，再执行下面的内容
        });
        return data;
    }

    $('#tree').treeview({
        data: getTree(), //获取数据
        levels: 1, //打开层级
        color: "#428bca",//颜色
        onNodeSelected: function (event, node) {
            console.log(node);
            //根据选择车系，加载对应的信息
            $.ajax({
                url: projectName + '/car/list.do',
                data: {
                    brandId: node.brandId,
                    seriesId: node.seriesId
                },
                dataType: "json",
                success: function (result) {
                    if (result.code == 200) {

                        $("#carBody").empty(); //请求table内容

                        var carList = result.data; //获取所有的数据
                        $.each(carList, function (index, car) {

                            var tr = "<tr/>";
                            tr += "<td>" + car.carId + "</td>";
                            tr += "<td>" + car.carBrand.brandName + "</td>";
                            tr += "<td>" + car.carSeries.seriesName + "</td>";
                            tr += "<td>" + car.carName + "</td>";
                            tr += "<td>" + car.carPrice + "</td>";
                            tr += "<td>" + car.carTime + "</td>";
                            tr += "<td><button class='btn btn-info btn-xs editClick' v='"+car.carId+"'>修改</button>" +
                                "<button class='btn btn-danger btn-xs deleteClick' v='"+car.carId+"'>删除</button></td>";
                            tr += "</tr>";

                            $(tr).appendTo($("#carBody"));
                        })
                    }
                }
            });
        },//事件：当选择节点的时候触发
    });


    /**
     * 点击添加车系的下拉框
     */
    initBrandList();


    $("#get_car_brand_id").change(function () {
        var brandId = $(this).val();
        console.log("新增车系ID：" + brandId);


        //1.判断选择的ID 和集合中的ID是否匹配
        $.each(brandSeriesList, function (index, brand) {

            //2.如果匹配 获取车系
            if (brandId == brand.brandId) {
                //3.车系放入车系下拉框
                setSeries(brand);
                return;
            }

        })


    });

});


function initBrandList() {
    $.getJSON(projectName + "/repertory/brand/list.do", function (result) {
        if (result.code == 200) {
            var brandList = result.data;
            brandSeriesList = result.data;
            $("#get_brand_id").empty(); //添加车系
            $("#get_car_brand_id").empty(); //添加车辆

            var first = true; //是否是第一次
            $.each(brandList, function (index, brand) {
                $("<option value=" + brand.brandId + ">" + brand.brandName + "</option>").appendTo($("#get_brand_id")); //添加车系
                $("<option value=" + brand.brandId + ">" + brand.brandName + "</option>").appendTo($("#get_car_brand_id")); //添加车辆

                //加载车系列表
                if (first) {
                    setSeries(brand);
                    first = false;
                }
            })
        }
    });
};

/**
 * 设置车系信息
 * @param brand
 */
function setSeries(brand) {
    var sericesList = brand.carSeriesList;
    $("#get_series_id").empty();
    $.each(sericesList, function (index, series) {
        $("<option value=" + series.seriesId + ">" + series.seriesName + "</option>").appendTo($("#get_series_id")); //添加车辆
    });
}